
import axios from 'axios'
import {domain} from "../api_server/common/config";
import {notification} from 'ant-design-vue'

const service = axios.create({
  baseURL: domain,
  timeout: 6000
})

const err = (error) => {
  let data = error.response.data
  if (error.response) {
    switch (error.response.status) {
      case 500:
        notification.error({
          message: '系统提示',
          description: data.result,
          duration: 4
        })
        break
      case 404:
        notification.error({ message: '系统提示', description:'很抱歉，资源未找到!',duration: 4})
        break
      case 403:
        notification.error({ message: '系统提示', description: '拒绝访问',duration: 4})
        break
      case 401:
        notification.error({ message: '系统提示', description:'未授权，请重新登录',duration: 4})
        break
      default:
        break
    }
  }
  return Promise.reject(error)
};

// request interceptor
service.interceptors.request.use(config => {
  return config
},(error) => {
  return Promise.reject(error)
})

// response interceptor
service.interceptors.response.use((response) => {
  return response.data
}, err)


export {
  service as axios
}

